package org.peterbjornx.gamestate;

import org.peterbjornx.Client;
import org.peterbjornx.util.TextUtils;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 8, 2009
 * Time: 8:45:27 PM
 * Handles the login screen
 */
public class LoginGameState extends GameState{
    private Image loginback;
    private Image loginbutton;
    private Rectangle button1clip;
    private Rectangle button2clip;
    private int state;
    private static final int WELCOME_SCREEN = 0;
    private static final int CREDITS_SCREEN = 1;
    private static final int LOGIN_SCREEN = 2;
    private String username = "";
    private String password = "";
    private int inputstate;
    private static final int INPUT_USERNAME = 0;
    private static final int INPUT_PASSWORD = 1;


    public LoginGameState(Client clientinstance) {
        super(clientinstance);
        try {
            loginback = myClient.titleimgreader.getImage("TITLEBOX",0);
            loginbutton = myClient.titleimgreader.getImage("TITLEBUTTON",0);
            int x = 202;
            int y = 171;
            button1clip = new Rectangle(x+24,y+130,loginbutton.getWidth(myClient),loginbutton.getHeight(myClient));
            button2clip = new Rectangle(x+190,y+130,loginbutton.getWidth(myClient),loginbutton.getHeight(myClient));
            state = WELCOME_SCREEN;
            inputstate = INPUT_USERNAME;
        } catch (IOException e) {
            System.err.println("Error initializing LoginGameState");
        }
    }

    public void addMessage(String s) {
        /*NOTING*/
    }

    public void paint(Graphics g) {
        myClient.drawtitleback(g);
        int x = 202;
        int y = 171;
        int bcenter = loginbutton.getWidth(myClient)/2;
        int lcenter = loginback.getWidth(myClient)/2;
        g.drawImage(loginback,x,y,myClient);
        if (state == WELCOME_SCREEN || state == LOGIN_SCREEN){
            g.drawImage(loginbutton,x+24,y+130,myClient);
            g.drawImage(loginbutton,x+190,y+130,myClient);
        }
        if (state == WELCOME_SCREEN){
            g.setColor(Color.yellow);
            int b1textx = x + 24 + bcenter - (int) g.getFontMetrics().getStringBounds("Credits",g).getWidth()/2;
            int btexty = y + 130 + 6 + g.getFontMetrics().getHeight();
            int b2textx = x + 190 + bcenter - (int) g.getFontMetrics().getStringBounds("Login",g).getWidth()/2;
            g.drawString("Credits",b1textx,btexty);
            g.drawString("Login",b2textx,btexty);

        }
        if (state == LOGIN_SCREEN){
            g.setColor(Color.yellow);
            int b1textx = x + 24 + bcenter - (int) g.getFontMetrics().getStringBounds("Main Menu",g).getWidth()/2;
            int btexty = y + 130 + 6 + g.getFontMetrics().getHeight();
            int b2textx = x + 190 + bcenter - (int) g.getFontMetrics().getStringBounds("Login",g).getWidth()/2;
            g.drawString("Main Menu",b1textx,btexty);
            g.drawString("Login",b2textx,btexty);
            int utextx = x + lcenter - (int) g.getFontMetrics().getStringBounds("Username:"+username,g).getWidth()/2;
            int utexty = y + 70;
            if (inputstate == INPUT_USERNAME)
                g.setColor(Color.white);
            else
                g.setColor(Color.yellow);
            g.drawString("Username:"+username,utextx,utexty);
            utexty+=g.getFontMetrics().getHeight();
            utextx = x + lcenter - (int) g.getFontMetrics().getStringBounds("Password:"+TextUtils.asterisks(password),g).getWidth()/2;
            if (inputstate == INPUT_PASSWORD)
                g.setColor(Color.white);
            else
                g.setColor(Color.yellow);
            g.drawString("Password:"+ TextUtils.asterisks(password),utextx,utexty);
        }
    }
     public void notifyloaded() {
        
    }
    public void keyTyped(KeyEvent e) {
        if (state != LOGIN_SCREEN)
            return;
        int ins = inputstate;
        String opw = password;
        String oun = username;
        if (inputstate == INPUT_USERNAME) {
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && username.length() > 0 )
                username = username.substring(0,username.length()-1);
          //  else if (e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyChar() == '\t')
          //      inputstate = INPUT_PASSWORD;
            else if (e.getKeyChar() > 0 && e.getKeyChar() < 128 && e.getKeyChar() != '\t' && e.getKeyChar() != 8)
                username+=e.getKeyChar();
        } else if (inputstate == INPUT_PASSWORD){
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && password.length() > 0 )
                password = password.substring(0,username.length()-1);
            //else if (e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyChar() == '\t')
           //     inputstate = INPUT_USERNAME;
            else if (e.getKeyChar() > 0 && e.getKeyChar() < 128 && e.getKeyChar() != '\t' && e.getKeyChar() != 8)
                password+=e.getKeyChar();
        }
        if (!password.equals(opw) || !username.equals(oun) || inputstate != ins) myClient.repaint();
    }

    public void keyPressed(KeyEvent e) {
        if (state != LOGIN_SCREEN)
            return;
        int ins = inputstate;
        String opw = password;
        String oun = username;

        if (inputstate == INPUT_USERNAME) {
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && username.length() > 0 )
                username = username.substring(0,username.length()-1);
            else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
                username = "";
            else if (e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyChar() == '\t')
                inputstate = INPUT_PASSWORD;
        } else if (inputstate == INPUT_PASSWORD){
            if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && password.length() > 0 )
                password = password.substring(0,password.length()-1);
            else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
                password = "";
            else if (e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyChar() == '\t')
                inputstate = INPUT_USERNAME;
        }
        if (!password.equals(opw) || !username.equals(oun) || inputstate != ins) myClient.repaint();
    }

    public void keyReleased(KeyEvent e) {
        //Empty
    }

    public void mousePressed(MouseEvent e) {
        Point mousep = e.getPoint();
        if (button1clip.contains(mousep)&&(state == WELCOME_SCREEN || state == LOGIN_SCREEN)){
            if (state == LOGIN_SCREEN)
                state = WELCOME_SCREEN;
            else
                state = CREDITS_SCREEN;
        }
        if (button2clip.contains(mousep)&&(state == WELCOME_SCREEN || state == LOGIN_SCREEN)){
            if (state == LOGIN_SCREEN){
                myClient.loginhandler.setUsername(username); 
                myClient.loginhandler.setPassword(password);
                myClient.loginhandler.setType(false);
                myClient.loginhandler.setUID(123456789);
                try {
                    myClient.loginhandler.runLogin();
                } catch (IOException e1) {
                    System.err.println("Exception during login procedure");
                    e1.printStackTrace();
                }
                int r = myClient.loginhandler.getResult();
                if (r == 2){
                    myClient.notifylogin();
                }
            }
            if (state == WELCOME_SCREEN)
                state = LOGIN_SCREEN;
        }
        myClient.repaint();
    }

    public void mouseClicked(MouseEvent e) {
        //EMPTY
    }

    public void mouseReleased(MouseEvent e) {
        //EMPTY
    }

    public void mouseEntered(MouseEvent e) {
        //EMPTY
    }

    public void mouseExited(MouseEvent e) {
        //EMPTY
    }
}
